<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  props: {
    modelValue: { type: Number, required: true },
    min: { type: Number, default: 1 },
    max: { type: Number, default: 99999 },
    step: { type: Number, default: 1 },
    disabled: Boolean,
  },
  emits: {
    "update:modelValue": (value: number) => typeof value === "number",
  },
  setup(props, ctx) {
    const subClick = () => {
      if (props.disabled || props.modelValue === props.min) return;
      ctx.emit("update:modelValue", props.modelValue - props.step);
    };
    const addClick = () => {
      if (props.disabled || props.modelValue === props.max) return;
      ctx.emit("update:modelValue", props.modelValue + props.step);
    };
    return { subClick, addClick };
  },
});
</script>

<template>
  <div class="stepper">
    <div
      class="stepper-btn"
      :style="{ opacity: disabled || modelValue === min ? '0.1' : '1' }"
      @click="subClick"
    >
      <div class="stepper-horizontal"></div>
    </div>
    <div class="stepper-value">{{ modelValue }}</div>
    <div
      class="stepper-btn"
      style="background-color: #1bc76f"
      :style="{ opacity: disabled || modelValue === max ? '0.1' : '1' }"
      @click="addClick"
    >
      <div class="stepper-horizontal" style="background-color: #ffffff"></div>
      <div class="stepper-vertical"></div>
    </div>
  </div>
</template>

<style lang="less">
.stepper {
  display: flex;
  align-items: center;
  flex-shrink: 0;

  .stepper-btn {
    position: relative;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: unit(1, PX) solid #1bc76f;
  }

  .stepper-horizontal,
  .stepper-vertical {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 12px;
    height: unit(1, PX);
    background-color: #1bc76f;
    transform: translate(-50%, -50%);
  }

  .stepper-value {
    width: 29px;
    font-size: 14px;
    color: #333333;
    line-height: 20px;
    text-align: center;
    font-weight: bold;
  }

  .stepper-vertical {
    width: unit(1, PX);
    height: 12px;
    background-color: #ffffff;
  }
}
</style>
